#include "gamelibrary.h"
#include "ikarusdebug.h"

GameLibrary::GameLibrary()
{
	this->LoadLibrary();
}

void GameLibrary::ParseConf()
{	
	ifstream myfile ("library.cfg");
	if(myfile.is_open())
	{
		char buf[1025];
		while(!myfile.eof())
		{
			myfile.getline(buf, 1024);
			string line = string(buf);
			string empty;
			unsigned int pos = line.find_first_of(" ");
			unsigned int eqpos = line.find("=");
			if(pos != string::npos && line.length() > pos + 1)
			{
				string varname = line.substr(0, pos);
				string tvalue = line.substr(eqpos+1);
				string value = trim(tvalue);
				if(varname.find("Game") != string::npos)
				{
					TempGameName = value.c_str();
					dprintf("Found Game:%s\n", TempGameName.c_str());
					continue;
				}
				if(varname.find("Picture") != string::npos)
				{
					TempGamePic = value.c_str();
					dprintf("Found Picture:%s\n", TempGamePic.c_str());
					this->AddGame();
					continue;
				}
			}
		}
	}
}

void GameLibrary::LoadLibrary()
{
	this->ParseConf();
}

void GameLibrary::AddGame()
{
LatestGameNr = Games.size();
Games[LatestGameNr+1] = std::make_pair(TempGameName.c_str(), TempGamePic.c_str());
dprintf("Games[%i] --> Game: %s Picture: %s\n", Games.find(LatestGameNr+1)->first, Games.find(LatestGameNr+1)->second.first.c_str(), Games.find(LatestGameNr+1)->second.second.c_str());
}
